//
//  VideoHelper.m
//  fastwatch
//
//  Created by lanou3g on 15/11/2.
//  Copyright © 2015年 songhaowen. All rights reserved.
//

#import "VideoHelper.h"
#import "SingleVideo.h"
#define client_id 51efa7f614d5a3ca//拼接网址头




static VideoHelper *videohelper = nil;
@implementation VideoHelper


#pragma mark 请求~解析工具单例
+(VideoHelper*)shareInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        videohelper = [[VideoHelper alloc] init];
    });
    return videohelper;
}

#pragma mark 请求方法~(传入需要请求的视屏类型名,个数)
-(void)downLoadDataFinished:(Result)result WithName:(NSString*)category withDataCount:(int)dataCount{
     //子线程网络请求
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
    
    NSURLSession *session = [NSURLSession sharedSession];
    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:category];//传入的名字(如:综艺)
    NSString *category_new = [category stringByAddingPercentEncodingWithAllowedCharacters:set];
    NSString *url = [NSString stringWithFormat:@"https://openapi.youku.com/v2/videos/by_category.json?client_id=51efa7f614d5a3ca&count=%d&category=%@&orderby=view-count&period=today",dataCount,category_new];//传入的名字和个数~拼接成url
    
    NSURLSessionTask *task = [session dataTaskWithURL:[NSURL URLWithString:url]completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        NSDictionary *bigDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *videos = bigDict[@"videos"];
        for (NSDictionary *dict in videos) {
            //1.拿到一个session
            NSURLSession *session_single = [NSURLSession sharedSession];
            //2.拿到一个id
            NSString *video_id = dict[@"id"];
            NSString *url_single = [NSString stringWithFormat:@"https://openapi.youku.com/v2/videos/show_basic.json?client_id=51efa7f614d5a3ca&video_id=%@",video_id];
            
            NSURLSessionTask *task_single = [session_single dataTaskWithURL:[NSURL URLWithString:url_single] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                NSDictionary *dict_single = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                //NSLog(@"%@", dict_single);
                
                SingleVideo *contenModel = [[SingleVideo alloc] init];
                [contenModel setValuesForKeysWithDictionary:dict_single];
                
                [self.contentArray addObject:contenModel];
                
                
                //主线程刷新ui,调用block
                dispatch_async(dispatch_get_main_queue()
                               , ^{
                                   result(); //例:[self.tableView reloadData];
                               });
               }];
            [task_single resume];
        }
    }];
    [task resume];
        
        });
}

//懒加载
- (NSMutableArray *)contentArray
{
    if (_contentArray == nil) {
        _contentArray = [NSMutableArray array];
    }
    return _contentArray;
}
@end
